package com.mingqijia.gassafety.console.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.db.entity.Organization;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.webserver.request.CreateDictionaryItemRequest;
import com.mingqijia.gassafety.webserver.request.UpdateDictionaryItemRequest;
import com.mingqijia.gassafety.webserver.response.OrganizationResponse;
import com.mingqijia.gassafety.webserver.service.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 组织层级关系 前端控制器
 * </p>
 *
 * @author li.shiying
 * @since 2023-07-24
 */
@RestController
@RequestMapping("business/v1/organization")
@Slf4j
@Api(tags = "组织数据")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    /**
     * 获取组织下拉列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation("获取组织下拉列表")
    public CommonResponse<List<OrganizationResponse>> getList(){
        String spId = ApplicationUtils.getWorkingSpId();
        List<OrganizationResponse> list =  organizationService.getList(spId);
        return CommonResponse.success(list);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("数据字典-组织数据汇总-新增类型")
    public CommonResponse<String> add(@RequestBody @Valid CreateDictionaryItemRequest req){
        log.info("add req："+req);
        return CommonResponse.success("添加成功",organizationService.add(req));
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ApiOperation("数据字典-组织数据汇总-删除类型")
    public CommonResponse<String> remove(@RequestParam String itemId){
        log.info("remove req："+itemId);
        organizationService.remove(itemId);
        return CommonResponse.success("删除成功");
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation("数据字典-组织数据汇总-编辑类型")
    public CommonResponse<String> edit(@RequestBody @Valid UpdateDictionaryItemRequest req){
        log.info("edit req："+req);
        organizationService.edit(req);
        return CommonResponse.success("编辑成功");
    }

    @RequestMapping(value = "/childList", method = RequestMethod.POST)
    @ApiOperation("数据字典-组织数据汇总-子组织列表(详情或编辑时查询)")
    public CommonResponse<List<Organization>> childList(@RequestParam @ApiParam(value = "父级spId(字典分类中类型编码)") String parentId){
        log.info("childList req："+parentId);
        return CommonResponse.success(organizationService.getChildList(parentId));
    }
}

